package com.example.yls.demoa;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Calendar;

import static java.util.Calendar.HOUR;
import static java.util.Calendar.MINUTE;

public class MainActivity extends AppCompatActivity
        /*implements IOnUpdateListener*/{
        private TextView txtTime;
        private MyReceiver receiver;
        private Button btn_1;
        private Button btn_2;


        @Override
        protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("demoa");
        initBunder();
         /*IntentFilter filter=new IntentFilter();
         filter.addAction("weather");
         filter.addAction("traffic");
         registerReceiver(receiver,filter);*/
            btn_1Event();
            btn_2Event();
      /*  receiver =new MyReceiver(MainActivity.this);
        IntentFilter filter =new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
            registerReceiver(receiver,filter);
        txtTime = findViewById(R.id.txt_time);*/
        /*UpdateTime();*/
    }

    private void btn_2Event() {

        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("traffic");
                intent.putExtra("TRAFFIC","堵车");
                intent.putExtra("TIME",25);
                sendBroadcast(intent);
            }
        });

        }

    private void btn_1Event() {

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("weather");
                intent.putExtra("WEATHER","晴天");
                intent.putExtra("WENDU",25);
                sendBroadcast(intent);
            }
        });

    }

    private void initBunder() {
        btn_1=findViewById(R.id.btn_1);
        btn_2=findViewById(R.id.btn_2);
        receiver=new MyReceiver();
    }


 /*   protected void onDestroy() {
            unregisterReceiver(receiver);
        super.onDestroy();
    }*/

/*    @Override
    public void UpdateTime() {
        Calendar calendar=Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int min =calendar.get(Calendar.MINUTE);
        StringBuffer buffer=new StringBuffer();
        if(hour<10){
            buffer.append("0");
        }
        buffer.append(hour);
        buffer.append(":");

        if(min<10){
            buffer.append("0");
        }
        buffer.append(min);

        txtTime.setText(hour+":"+min);
    }*/
}
